package com.ebeitech.land.auction.wxapi;

import android.content.Context;
import android.os.AsyncTask;

import com.ebeitech.auction.application.EbeiApplication;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Random;

/**
 * Created by alice on 2017/11/15.
 */
public class WeiXinUtil {

	public static final String TAG = "WeiXinUtil";
	public static final String ACTION_WEIXIN_LOGIN = "ACTION_WEIXIN_LOGIN";

	private static final String APP_ID = "wx86f2f5b78067f0e6";

	private Context context;

	private static WeiXinUtil mWeiXinUtil;
	private static IWXAPI api;

	public static WeiXinUtil getInstance() {
		if (mWeiXinUtil == null) {
			synchronized (WeiXinUtil.class) {
				if (mWeiXinUtil == null) {
					mWeiXinUtil = new WeiXinUtil();
				}
			}
		}
		return mWeiXinUtil;
	}

	public IWXAPI getIWXAPI() {
		return api;
	}

	private WeiXinUtil() {
		api = WXAPIFactory.createWXAPI(EbeiApplication.getContext(), APP_ID);
	}

	public void doLogin() {
		new AsyncTask<Void, Void, Void>() {
			@Override
			protected Void doInBackground(Void... params) {
				final SendAuth.Req req = new SendAuth.Req();
				req.scope = "snsapi_userinfo";
				req.state = "wechat_sdk_" + new Random(100).nextInt();
				api.sendReq(req);
				return null;
			}
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	public void doLogout() {
		api.unregisterApp();
	}

}
